﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKCodeBase.WinForm;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.DbOpening;
using YJKOpeningCommon.OpeningProfile;
using YJKOpeningCommon.Public;
using YJKOpeningDesignInput.Composite;
using YJKOpeningDesignInput.Public;

namespace YJKOpeningDesignInput.ConfigForm
{
	public partial class OpeningDesignInputReportForm : ConfigDataBaseForm
	{
		public OpeningDesignInputReportForm(LinkedList<DbOpeningBase> lstDbOpening, ExternalCommandData cmdData, WindowHandle handle)
		{
			this.InitializeComponent();
			base.SetFormLocation(this.strXmlPath);
			WinDPIManager.GetWinFormXYDpi(ref this.m_dXScale, ref this.m_dYScale);
			this.m_iNormalHeight = (this.m_iHeight = base.Height);
			this.m_iNormalWidth = (this.m_iWidth = base.Width);
			this.panel1.Size = new Size(this.panel1.Width, Convert.ToInt32(179.0 * this.m_dYScale));
			this.panel2.Location = new System.Drawing.Point(0, Convert.ToInt32(182.0 * this.m_dYScale));
			this.panel3.Location = new System.Drawing.Point(0, Convert.ToInt32(209.0 * this.m_dYScale));
			this.BtShrink.Location = new System.Drawing.Point(Convert.ToInt32(282.0 * this.m_dXScale), Convert.ToInt32(211.0 * this.m_dYScale));
			this.m_lstDbOpening = lstDbOpening;
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_cmdData = cmdData;
			this.m_hWndRevit = handle;
			base.Height = Convert.ToInt32(485.0 * this.m_dYScale);
			this.m_iInitExWidth = Convert.ToInt32(900.0 * this.m_dXScale);
			this.m_iInitUnExWidth = Convert.ToInt32(330.0 * this.m_dXScale);
			this.dataGridView.RowHeadersVisible = false;
			this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dataGridView.MultiSelect = false;
			this.SetDataGridViewColumn();
			base.Width = this.m_iInitUnExWidth;
		}

		private void BtShrink_Click(object sender, EventArgs e)
		{
			if (this.m_isShrink)
			{
				this.BtShrink.Text = "<--";
				this.dataGridView.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
				this.m_isShrink = false;
				this.m_isClickBtShrink = false;
				this.panel1.Height += this.panel2.Height;
				this.panel2.Location = new System.Drawing.Point(this.panel2.Location.X, this.panel2.Location.Y + this.panel2.Height);
				int x = base.Width - this.BtShrink.Width - Convert.ToInt32(15.0 * this.m_dXScale);
				int y = this.panel3.Location.Y;
				this.BtShrink.Location = new System.Drawing.Point(x, y);
				int x2 = this.BtShrink.Left - this.panel3.Width - Convert.ToInt32(3.0 * this.m_dXScale);
				this.panel3.Location = new System.Drawing.Point(x2, y);
				this.SetDataGridViewColumn();
				base.Width = this.m_iInitExWidth;
				return;
			}
			this.BtShrink.Text = "-->";
			this.m_isShrink = true;
			this.m_isClickBtShrink = true;
			this.panel1.Height -= this.panel2.Height;
			this.panel2.Location = new System.Drawing.Point(this.panel2.Location.X, this.panel2.Location.Y - this.panel2.Height);
			int y2 = this.panel2.Location.Y + this.panel2.Height + Convert.ToInt32(3.0 * this.m_dYScale);
			this.panel3.Location = new System.Drawing.Point(this.panel2.Location.X, y2);
			int x3 = Convert.ToInt32(this.panel2.Location.X + this.panel2.Width + 112);
			this.BtShrink.Location = new System.Drawing.Point(x3, this.panel3.Location.Y + this.BtSet.Location.Y);
			this.SetDataGridViewColumn();
			base.Width = this.m_iInitUnExWidth;
		}

		private void OpeningDesignInputReportForm_Resize(object sender, EventArgs e)
		{
			if (base.Width < this.m_iNormalWidth || base.Height < this.m_iNormalHeight)
			{
				base.Width = this.m_iNormalWidth;
				base.Height = this.m_iNormalHeight;
				return;
			}
			this.SetDataGridViewColumn();
			int dataGridViewWidth;
			if (!this.m_isShrink)
			{
				dataGridViewWidth = base.Width - this.m_iInitExWidth;
			}
			else
			{
				dataGridViewWidth = base.Width - this.m_iInitUnExWidth;
			}
			int num = base.Height - this.m_iHeight;
			this.panel1.Height += num;
			int y = this.panel2.Location.Y + num;
			this.panel2.Location = new System.Drawing.Point(this.panel2.Location.X, y);
			int y2 = this.panel3.Location.Y + num;
			int y3 = this.BtShrink.Location.Y + num;
			if (this.m_isShrink)
			{
				this.panel3.Location = new System.Drawing.Point(this.panel3.Location.X, y2);
				this.BtShrink.Location = new System.Drawing.Point(this.BtShrink.Location.X, y3);
			}
			else
			{
				int num2 = base.Width - this.m_iWidth;
				int x = this.panel3.Location.X + num2;
				int x2 = this.BtShrink.Location.X + num2;
				this.panel3.Location = new System.Drawing.Point(x, y2);
				this.BtShrink.Location = new System.Drawing.Point(x2, y3);
			}
			if (!this.m_isFirstChange)
			{
				this.m_iWidth = base.Width;
				this.m_iHeight = base.Height;
				this.m_isFirstChange = true;
				return;
			}
			if (!this.m_isClickBtShrink)
			{
				this.SetDataGridViewWidth(dataGridViewWidth);
			}
			else
			{
				this.m_isClickBtShrink = false;
			}
			this.m_iWidth = base.Width;
			this.m_iHeight = base.Height;
		}

		private void SetDataGridViewWidth(int widthGrow)
		{
			int num;
			if (this.m_isShrink)
			{
				num = widthGrow / 4;
			}
			else
			{
				num = widthGrow / 9;
			}
			bool flag = true;
			for (int i = 9; i >= 1; i--)
			{
				if (this.dataGridView.Columns[i].Visible)
				{
					if (flag && !this.m_isShrink)
					{
						this.dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
						flag = false;
					}
					else
					{
						this.dataGridView.Columns[i].Width += num;
					}
				}
			}
		}

		private void SetDataGridViewColumn()
		{
			this.dataGridView.Columns[0].Width = Convert.ToInt32(25.0 * this.m_dXScale);
			this.dataGridView.Columns[1].Width = Convert.ToInt32(35.0 * this.m_dXScale);
			this.dataGridView.Columns[2].Width = Convert.ToInt32(35.0 * this.m_dXScale);
			this.dataGridView.Columns[6].Width = Convert.ToInt32(120.0 * this.m_dXScale);
			this.dataGridView.Columns[7].Width = Convert.ToInt32(120.0 * this.m_dXScale);
			this.dataGridView.Columns[3].Width = Convert.ToInt32(80.0 * this.m_dXScale);
			this.dataGridView.Columns[4].Width = Convert.ToInt32(80.0 * this.m_dXScale);
			this.dataGridView.Columns[5].Width = Convert.ToInt32(80.0 * this.m_dXScale);
			this.dataGridView.Columns[8].Width = Convert.ToInt32(120.0 * this.m_dXScale);
			this.dataGridView.Columns[9].Width = Convert.ToInt32(100.0 * this.m_dXScale);
			if (this.m_isShrink)
			{
				this.dataGridView.Columns[3].Visible = false;
				this.dataGridView.Columns[4].Visible = false;
				this.dataGridView.Columns[5].Visible = false;
				this.dataGridView.Columns[8].Visible = false;
				this.dataGridView.Columns[9].Visible = false;
				this.dataGridView.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
				this.m_iNormalWidth = Convert.ToInt32(330.0 * this.m_dXScale);
				return;
			}
			this.dataGridView.Columns[3].Visible = true;
			this.dataGridView.Columns[4].Visible = true;
			this.dataGridView.Columns[5].Visible = true;
			this.dataGridView.Columns[8].Visible = true;
			this.dataGridView.Columns[9].Visible = true;
			this.dataGridView.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
			this.dataGridView.Columns[9].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.m_iNormalWidth = Convert.ToInt32(900.0 * this.m_dXScale);
		}

		public void SetOpeningData2DataGridView()
		{
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
			Interface.GetXmlData(ref dictionary);
			IEnumerator enumerator = this.m_lstDbOpening.GetEnumerator();
			this.dataGridView.AllowUserToAddRows = true;
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				DbOpeningBase dbOpeningBase = obj as DbOpeningBase;
				EHostObject eHostObject = dbOpeningBase.m_eHostObject;
				int num = (int)eHostObject;
				string text = StrHostObj.m_strHostObj[num];
				ECutObject eCutObject = dbOpeningBase.m_eCutObject;
				num = (int)eCutObject;
				string text2 = StrCutObj.m_strCutObj[num];
				Interface.SetOpeningExternDist(ref dbOpeningBase, dictionary);
				string strAxisX = dbOpeningBase.m_strAxisX;
				string strAxisY = dbOpeningBase.m_strAxisY;
				string strFloorPlan = dbOpeningBase.m_strFloorPlan;
				string text3 = string.Empty;
				if ((dbOpeningBase.m_eCutObject == ECutObject.eHvPipe || dbOpeningBase.m_eCutObject == ECutObject.eWaterPipe || dbOpeningBase.m_eCutObject == ECutObject.ePipeMulti) && dbOpeningBase is DbRectWallOpening)
				{
					OpeningProfileRect openingProfileRect = dbOpeningBase.m_profile as OpeningProfileRect;
					text3 = AssistFunc.feetToMM(openingProfileRect.m_cutHeight).ToString("f0");
				}
				else
				{
					text3 = dbOpeningBase.GetCutSize(false);
				}
				string text4 = dbOpeningBase.GetOpeningSize(false);
				string text5 = DateTime.Now.ToString();
				this.dataGridView.Rows.Add(new object[]
				{
					this.dataGridView.RowCount,
					strAxisX,
					strAxisY,
					text,
					text2,
					strFloorPlan,
					text3,
					text4,
					text5
				});
				this.dataGridView.Rows[this.dataGridView.RowCount - 2].Tag = dbOpeningBase;
			}
			this.dataGridView.AllowUserToAddRows = false;
		}

		private void dataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex == -1)
			{
				return;
			}
			DbOpeningBase dbOpeningBase = this.dataGridView.Rows[e.RowIndex].Tag as DbOpeningBase;
			CommonInterface.DoubleClick2Location(dbOpeningBase, this.m_uiDoc);
		}

		private void BtSet_Click(object sender, EventArgs e)
		{
			OpeningDesignSetReportForm openingDesignSetReportForm = new OpeningDesignSetReportForm();
			if (openingDesignSetReportForm.ShowDialog() == DialogResult.OK)
			{
				Dictionary<string, int> dictionary = new Dictionary<string, int>();
				Interface.GetXmlData(ref dictionary);
				GraphicsStyle graphicsStyle = null;
				CommonInterface.GetPreViewStyle(ref graphicsStyle, this.m_uiDoc.Document);
				PreViewScene preViewScene = null;
				if (graphicsStyle != null)
				{
					preViewScene = new PreViewScene(this.m_uiDoc.Document, graphicsStyle.Id, 0.32808398950131235);
				}
				for (int i = 0; i < this.dataGridView.RowCount; i++)
				{
					DbOpeningBase dbOpeningBase = this.dataGridView.Rows[i].Tag as DbOpeningBase;
					Interface.SetOpeningExternDist(ref dbOpeningBase, dictionary);
					DbOpeningBase dbOpeningBase2 = null;
					if (Interface.RefreshOpeningData(dbOpeningBase, dictionary, ref dbOpeningBase2) && dbOpeningBase2 != null)
					{
						this.dataGridView.Rows[i].Tag = dbOpeningBase2;
						this.m_lstDbOpening.Remove(dbOpeningBase);
						this.m_lstDbOpening.AddLast(dbOpeningBase2);
						Element element = null;
						Interface.DeleteModelGroup(this.m_uiDoc.Document, dbOpeningBase.m_idModelGroup);
						dbOpeningBase2.CreateOpeningPreviewInstance(out element, preViewScene);
						if (element != null)
						{
							dbOpeningBase2.m_idModelGroup = element.Id;
						}
						dbOpeningBase = dbOpeningBase2;
					}
					string empty = string.Empty;
					empty = dbOpeningBase.GetOpeningSize(false);
					this.dataGridView.Rows[i].Cells["openingSize"].Value = empty;
				}
			}
		}

		private void BtCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void OpeningDesignInputReportForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			Document document = this.m_uiDoc.Document;
			View3D view3D = document.ActiveView as Autodesk.Revit.DB.View3D;
			if (view3D != null && view3D.IsSectionBoxActive)
			{
				Transaction transaction = new Transaction(document, "SetSectionBox");
				transaction.Start();
				view3D.IsSectionBoxActive = false;
				transaction.Commit();
			}
			base.Location.X.ToString();
			base.Location.Y.ToString();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				if (File.Exists(this.strXmlPath))
				{
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					XmlReader xmlReader = XmlReader.Create(this.strXmlPath, xmlReaderSettings);
					xmlDocument.Load(xmlReader);
					XmlElement documentElement = xmlDocument.DocumentElement;
					documentElement.SetAttribute("LocationX", base.Location.X.ToString());
					documentElement.SetAttribute("LocationY", base.Location.Y.ToString());
					xmlReader.Close();
				}
				else
				{
					XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
					xmlDocument.AppendChild(newChild);
					XmlElement xmlElement = xmlDocument.CreateElement("HistoryData");
					xmlDocument.AppendChild(xmlElement);
					xmlElement.SetAttribute("LocationX", base.Location.X.ToString());
					xmlElement.SetAttribute("LocationY", base.Location.Y.ToString());
				}
				xmlDocument.Save(this.strXmlPath);
			}
			catch (Exception)
			{
				throw;
			}
			CommonInterface.KeyEsc(this.m_hWndRevit.Handle);
			CommonInterface.KeyEsc(this.m_hWndRevit.Handle);
		}

		private void BtComposite_Click(object sender, EventArgs e)
		{
			bool flag = false;
			CompositeHandle compositeHandle = new CompositeHandle(this.m_uiDoc);
			compositeHandle.CompositeDbOpening(ref this.m_lstDbOpening, ref flag);
			if (flag)
			{
				this.dataGridView.Rows.Clear();
				this.SetOpeningData2DataGridView();
				YJKMessageBox.Show("合并洞口成功");
				return;
			}
			YJKMessageBox.Show("无洞口需要合并");
		}

		private void BtnInput_Click(object sender, EventArgs e)
		{
			SelecteXmlPathForm selecteXmlPathForm = new SelecteXmlPathForm(ESelecteXmlPathForm.eDesignInput);
			if (selecteXmlPathForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			DbOpeningImpExpManager dbOpeningImpExpManager = new DbOpeningImpExpManager(selecteXmlPathForm.XmlPathName);
			dbOpeningImpExpManager.SetDesignUser(this.m_cmdData.Application.Application.Username);
			if (!dbOpeningImpExpManager.ExportOpening2Xml(this.m_lstDbOpening))
			{
				return;
			}
			YJKMessageBox.Show("提资成功");
		}

		private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dataGridView.Columns[e.ColumnIndex].Name != "note")
			{
				return;
			}
			int rowIndex = e.RowIndex;
			string strRemark = this.dataGridView.Rows[rowIndex].Cells["note"].Value as string;
			DbOpeningBase dbOpeningBase = this.dataGridView.Rows[rowIndex].Tag as DbOpeningBase;
			dbOpeningBase.m_strRemark = strRemark;
		}

		private void OpeningDesignInputReportForm_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					base.Close();
				}
			}
			catch
			{
			}
		}

		private int m_iWidth;

		private int m_iHeight;

		private int m_iNormalWidth;

		private int m_iNormalHeight;

		private bool m_isShrink = true;

		private bool m_isFirstChange;

		private bool m_isClickBtShrink;

		private int m_iInitExWidth;

		private int m_iInitUnExWidth;

		private LinkedList<DbOpeningBase> m_lstDbOpening;

		private UIDocument m_uiDoc;

		private ExternalCommandData m_cmdData;

		private WindowHandle m_hWndRevit;

		private double m_dXScale;

		private double m_dYScale;

		private string strXmlPath = Product.UserDataLocation + "\\OpeningDesignInputReportForm.xml";

		private IContainer components;
	}
}
